/*
   Copyright 2009 the original author or authors

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package com.googlecode.kohmori.springmvc;

import java.io.ByteArrayOutputStream;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor; 
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.googlecode.kohmori.reportgen.SimplePDFGenerator;

public class SimpleReportController extends SimpleFormController
{
    private SimplePDFGenerator simplePDFGenerator = null;
    protected static ArrayList<String> monthList = null;
    protected static ArrayList<String> quarterList = null;
    protected Connection conn = null;
    
    public void setSimplePDFGenerator(SimplePDFGenerator simplePDFGenerator) {
        this.simplePDFGenerator = simplePDFGenerator;
    }

    public SimplePDFGenerator getSimplePDFGenerator() {
        if (simplePDFGenerator == null) {
            throw new IllegalStateException("(Internal error) SimpleReportController" 
            + " has not been initialized with a SimplePDFGenerator.  See " +
                    "config file.");
        }
        return simplePDFGenerator;
    }
    
    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, 
            Object object, BindException errors) throws Exception {
       
        // obtain DB connection and parameter information
        HttpSession session = request.getSession(true);
        conn = (Connection) session.getAttribute("connection");
        DefaultReportParams rP = (DefaultReportParams) object;

        // generate Report and send to client
        SimplePDFGenerator pdfGen = getSimplePDFGenerator();
        ByteArrayOutputStream pdfStream = pdfGen.createReportBAOS(conn, rP);
        byte[] pdfByteArray = pdfStream.toByteArray();
        response.setHeader("Content-Disposition", "inline;filename=report.pdf");
        response.setHeader("Pragma", "private");
        response.setHeader("Cache-Control", "private");
        response.setContentType("application/pdf");
        response.setContentLength(pdfByteArray.length);
        ServletOutputStream out = response.getOutputStream();
        out.write(pdfByteArray);
        out.flush();
        return null;
    }

    /**
     * Set up a custom property editor for the application's date format.
     * This method converts string dates on the HTML form to java.util.Dates
     * within the POJO.
     */
    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true, 10));
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
        binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, true));
    }   
    
    static { 
        // Create a list of months for drop-down menu items.
        monthList = new ArrayList<String>();
        monthList.add("JAN");
        monthList.add("FEB");
        monthList.add("MAR");
        monthList.add("APR");
        monthList.add("MAY");
        monthList.add("JUN");
        monthList.add("JUL");
        monthList.add("AUG");
        monthList.add("SEP");
        monthList.add("OCT");
        monthList.add("NOV");
        monthList.add("DEC");
        quarterList = new ArrayList<String>();
        quarterList.add("1");
        quarterList.add("2");
        quarterList.add("3");
        quarterList.add("4");
    }       
        
}
